package traverser;

import java.util.HashMap;

public class RepetitionTracker {

	protected HashMap<String, Integer> keyWords;
	protected int keysSeen = 0;

    public RepetitionTracker () {
        keyWords = new HashMap<String, Integer>();
    }


	public double computeRepetition (String link) {
		String [] keys = KeyTracker.splitAndReduce(link);

		// Next, calculate the repetition of this page
		double repetition = 0;
		for (String key : keys) {
			int reps = keyWords.get(key);
			repetition += reps * reps;
		}
		repetition /= keysSeen;
		repetition = Math.min(repetition, 0.9);
		repetition = 1 - repetition;
		return repetition;
	}

	public void recordRepetition (String link) {
		String [] keys = KeyTracker.splitAndReduce(link);
		for (String key : keys) {
			keysSeen++;
			Integer reps;
			if ((reps = keyWords.get(key)) == null) {
				keyWords.put(key, 1);
			} else {
				keyWords.put(key, reps + 1);
			}
		}
	}
}

